from kiwi.ui.delegates import GladeSlaveDelegate

from otica.views.baseeditor import BaseEditor
from otica.models.usuarios import Usuario, Perfil

acesso_widgets = ['login','senha']

class AcessoEditor(BaseEditor):
    
    def __init__(self, excluir):
        
        BaseEditor.__init__(self,excluir,Usuario,'login', AcessoSlave, acesso_widgets)
        
        self.baseeditor.move(280,260)
        self.localizar_action.set_visible(False)
        self.tabela_aceso = {1:self.editor.permissoescheckbutton,
                             2:self.editor.excluircheckbutton}
        self.editor.login.set_editable(False)
        self.update_usuario()
        #self.editor.focus_toplevel()
        
        
    def limpa_permissoes(self):
        
        self.editor.permissoescheckbutton.set_active(True)
        self.editor.excluircheckbutton.set_active(True)
    
    def update_permissoes(self):
        permissoes = self.model.permissoes
        ids_permissoes = [permissao.id for permissao in permissoes]

        for id,componente in self.tabela_aceso.items():
            if id in ids_permissoes:
                componente.set_active(False)
        
    def update_perfil(self):
        
        perfil = self.model.perfil
        perfis = Perfil.select(connection=self.connect, orderBy='descricao')
        items = [(p.descricao, p.id) for p in perfis]
        self.editor.perfilcombobox.prefill(items)
        self.editor.perfilcombobox.select_item_by_data(perfil.id)
                    
    
    def update_usuario(self):
        
        if self.model:
            self.update_perfil()
            self.limpa_permissoes()
            self.update_permissoes()
        else:
            self.editor.login.set_text('')
            self.editor.senha.set_text('')
            self.limpa_permissoes()        
        
    def update_model(self):
        
        self.model.perfil = self.editor.perfilcombobox.get_selected_data()
        permissoes = [id for id,componente in self.tabela_aceso.items()
                      if componente.get_active() == False]
        self.model.update_permissoes(permissoes)
                 
        
    def on_novo_action__activate(self, action):
        
        self.editor.login.set_editable(True)
        BaseEditor.on_novo_action__activate(self, action)
        self.update_usuario()
        self.editor.login.grab_focus()

        
    def on_editar_action__activate(self, action):
        BaseEditor.on_editar_action__activate(self, action)
        self.editor.senha.grab_focus()
        
    def set_editable_widgets(self,editable):
        self.editor.perfilcombobox.set_sensitive(editable)
        self.editor.senha.set_sensitive(editable)
        self.editor.excluircheckbutton.set_sensitive(editable)
            
    def on_desfazer_action__activate(self, action):
        
        BaseEditor.on_desfazer_action__activate(self, action)
        self.update_usuario()

    def on_salvar_action__activate(self, action):
        self.update_model()
        BaseEditor.on_salvar_action__activate(self, action)
            
    def on_avancar_action__activate(self, action):

        BaseEditor.on_avancar_action__activate(self, action)
        self.update_usuario()
        
    def on_voltar_action__activate(self, action):

        BaseEditor.on_voltar_action__activate(self, action)
        self.update_usuario()

    def on_primeiro_action__activate(self, action):

        BaseEditor.on_primeiro_action__activate(self, action)
        self.update_usuario()

    def on_ultimo_action__activate(self, action):

        BaseEditor.on_ultimo_action__activate(self, action)
        self.update_usuario()
        
    def on_excluir_action__activate(self, action):
        BaseEditor.on_excluir_action__activate(self, action)
        self.update_usuario()

class AcessoSlave(GladeSlaveDelegate):
    def __init__(self):
        GladeSlaveDelegate.__init__(self,gladefile="acessoeditor")
    
    def on_perfilcombobox__changed(self, combo):
        if combo.get_selected_data() == 1:
            self.permissoescheckbutton.set_active(False)
            self.excluircheckbutton.set_active(False)
        else:
            self.permissoescheckbutton.set_active(True)
            self.excluircheckbutton.set_active(False)
        